package wiki.liven.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import wiki.liven.pojo.dto.DataDto;
import wiki.liven.pojo.dto.ReturnJosn;
import wiki.liven.service.IClickDataService;
import wiki.liven.utils.TimeUtils;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by qiangjun on 2017/3/23.
 * 点播统计服务
 */
@RestController
@RequestMapping("/doxData")
@CrossOrigin
public class ClickDataController {


    @Autowired
    private IClickDataService service;


    /**
     * 处理时间太长了。！！！！！！！！！！！！！！！！！！！！！！！！！！！
     * 展示指定用户的点播时长
     * @param mac 用户信息
     * @param startTime 统计起始时间
     * @param endTime 统计结束时间
     * @return
     */
    @GetMapping("/userTimeOnTv")
    public ReturnJosn showUserTimeOnTv(@RequestParam String mac,@RequestParam String startTime,
                                       @RequestParam String endTime,
                                       @RequestParam String type) throws ParseException {
        ReturnJosn returnJosn = new ReturnJosn();
        returnJosn.setCode("200");
        List<DataDto> dataDtos = new ArrayList<>();
        List<String> times = TimeUtils.slicedTime(startTime,endTime,type);
        if (null!=mac&&!"".equals(mac)){
            for (int i = 0;i<=times.size()-2;i++){
                DataDto dataDto = new DataDto();
                String _startTime = times.get(i);
                String _endTime = times.get(i+1);
                dataDto.setDate(times.get(i).split(" ")[0]);
                dataDto.setValue(service.getClickTime(mac,_startTime,_endTime));
                dataDtos.add(dataDto);
            }
        }else{
            List<String> users;
            Long timeCount = 0L;
            for (int i = 0;i<=times.size()-2;i++){
                DataDto dataDto = new DataDto();
                String _startTime = times.get(i);
                String _endTime = times.get(i+1);
                dataDto.setDate(_startTime.split(" ")[0]);
                users = service.getUsers(_startTime,_endTime);
                for(String user:users){
                    timeCount += service.getClickTime(user,_startTime,_endTime);
                    System.out.println("统计过程");
                    System.out.println("时间区段-"+_startTime+","+_endTime+"---mac-"+user+"---统计数据-"+timeCount);
                }
                dataDto.setValue(timeCount);
                dataDtos.add(dataDto);
            }
        }
        returnJosn.setData(dataDtos);
        return returnJosn;
    }


    /**
     * 展示活跃用户数量
     * @param startTime
     * @param endTime
     * @return
     */
    @GetMapping("/activeUser")
    public ReturnJosn showActivieUser(@RequestParam String startTime,
                                      @RequestParam String endTime,
                                      @RequestParam String type) throws ParseException {
        ReturnJosn returnJosn = new ReturnJosn();
        returnJosn.setCode("200");
        returnJosn.setData(splictTime(startTime,endTime,type));
        return returnJosn;
    }


    /**
     * 查询用户数量
     * 时间区段分割后的时间
     * @param startTime
     * @param endTime
     * @param type
     * @return
     * @throws ParseException
     */
    public List<DataDto> splictTime(String startTime,String endTime,String type) throws ParseException {
        List<String> times = TimeUtils.slicedTime(startTime,endTime,type);
        System.out.println("时间区段:"+times.toString());
        List<DataDto> dataDtos = new ArrayList<>();
        for (int i = 0;i<=times.size()-2;i++){
            DataDto dataDto = new DataDto();
            String _startTime = times.get(i);
            String _endTime = times.get(i+1);
            List<String> users = service.getUsers(_startTime,_endTime);
            System.out.println("用户总数："+users.size());
            dataDto.setDate(times.get(i).split(" ")[0]);
            dataDto.setValue((long)service.selectActivityUser(users, _startTime,_endTime).size());
            dataDtos.add(dataDto);
        }
        return dataDtos;
    }


    /**
     * 展示热心用户数量
     * @param startTime
     * @param endTime
     * @return
     */
    public ReturnJosn showHotUser(@RequestParam String startTime,
                                        @RequestParam String endTime,
                                        @RequestParam String type){
        ReturnJosn returnJosn = new ReturnJosn();
        return returnJosn;
    }








}
